package com.itheima.travel.config;

import com.itheima.travel.config.SpringMvcConfig;
import com.itheima.travel.interceptors.CrossInterceptor;
import com.itheima.travel.interceptors.LoginInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;

/**
 * SpringMVC的核心配置类
 *
 * @Description：声明spring-mvc的配置类
 */
@ComponentScan(value = {"com.itheima.travel.web",
                         "springfox.documentation.swagger.web"})
@Configuration
public class PlatformSpringMvcConfig extends SpringMvcConfig {

    /**
     * @Description 登录拦截配置
     */
    @Bean("loginInterceptor")
    public LoginInterceptor loginInterceptor() {
        return new LoginInterceptor();
    }

    /**
     * 资源路径 映射
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        /**
         * 支持webjars
         */
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        /**
         * 支持swagger
         */
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        super.addResourceHandlers(registry);
    }

    /**
     * 跨域拦截器
     * @return
     */
    @Bean("crossInterceptor")
    public CrossInterceptor getCrossInterceptor(){
        return new CrossInterceptor();
    }

    /**
     * @Description 给拦截器设置拦截的路径
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 配置跨域拦截器拦截的路径
        registry.addInterceptor(getCrossInterceptor())
                .addPathPatterns("/**");
        // 登录拦截
        registry.addInterceptor(loginInterceptor())
                .addPathPatterns("/**") // 设置拦截的路径
                .excludePathPatterns( // 设置排除的路径
                        "/**/user/**",  // 放行user模块下的所有
                        "/**/seller/**", // 放行seller模块下的所有
                        "/**/category/**", // 放行category模块下的所有
                        "/**/route/**", //放行线路模块下的所有功能
                        "/**/webjars/**", //  放行swagger静态资源不拦截
                        "/**/swagger-ui.html", // 放行swagger静态资源不拦截
                        "/**/swagger-resources/**", // 放行swagger静态资源不拦截
                        "/**/v2/**"  // 放行swagger静态资源不拦截
                );
    }
}